package gamejam.platformer;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import redsoxfan.gamejam.base.DesktopWrapper;
import redsoxfan.gamejam.base.Game;
import redsoxfan.gamejam.base.GameResource;

public class Platformer extends Game implements KeyListener {
	
	private boolean boundingBox;
	private Map map;
	private Player player;
	private String[] maps = {"map.txt", "map2.txt", "map3.txt"};
	private int mapIndex = 0;
	
	public static void main(String[] args) {
		new DesktopWrapper(new Platformer());
	}

	public String authors() {
		return "Brian Ashworth and Spencer Rak";
	}

	public String beforeApplet() {
		return infoBlock("http://3hrgamejam.com", "3hr Game Jam (Practice)", "April 29, 2014", "Control More Than One (LD Theme)", "Get to the end. We went with too complicated of an idea so the theme does not really show. This is an extended version.", "Use A and D to move left and right. Use W to jump. S will reverse gravity. NOTE: The bad moving platforms are not done.");
	}
	
	public void draw(Graphics g) {
		if (boundingBox) {
			map.drawBoundingBox(g);
			player.drawBoundingBox(g);
		} else {
			map.draw(g);
			player.draw(g);
		}
	}

	public Image icon() {
		return new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
	}

	public void init() {
		addKeyListener(this);
		try {
			map = new Map(GameResource.loadResource("/gamejam/platformer/" + maps[mapIndex]), width(), height());
			player = new Player(GameResource.loadImage("/gamejam/platformer/player_sprite.png", Map.TILE_SIZE, Map.TILE_SIZE), map);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	public void keyPressed(KeyEvent e) {
		player.keyPressed(e);
	}

	public void keyReleased(KeyEvent e) {
		if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_1)
			boundingBox = !boundingBox;
		else
			player.keyReleased(e);
	}

	public void keyTyped(KeyEvent e) {
		
	}

	public String shortTitle() {
		return "Platformer";
	}

	public void tick() {
		player.tick();
		map.center(player.getCenterX(), player.getCenterY());
		map.tick();
		if (map.getFinishTile().getBounds().intersects(player.getBounds())) {
			mapIndex++;
			mapIndex %= maps.length;
			try {
				map = new Map(GameResource.loadResource("/gamejam/platformer/" + maps[mapIndex]), width(), height());
				player = new Player(GameResource.loadImage("/gamejam/platformer/player_sprite.png", Map.TILE_SIZE, Map.TILE_SIZE), map);
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(0);
			}
		}
	}

	public String title() {
		return "Platformer";
	}
}
